package cn.edu.zut.hhr.controller;


import cn.edu.zut.hhr.entity.User;
import cn.edu.zut.hhr.exceptions.CheckException;
import cn.edu.zut.hhr.service.IUserService;
import cn.edu.zut.hhr.utils.CheckUtil;
import cn.edu.zut.hhr.utils.MDUtil;
import cn.edu.zut.hhr.utils.RestResponse;
import cn.edu.zut.hhr.utils.TokenUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author hhr
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    IUserService userService;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getUserByIdFallback", ignoreExceptions = CheckException.class)
    public RestResponse<User> getUserById(@PathVariable("id") Long id){
        User user = userService.getUserById(id);
        CheckUtil.notNull(user,"无当前用户");
        if(user != null){
            return RestResponse.success(user);
        } else{
            return RestResponse.fail("无当前用户");
        }
    }

    public RestResponse<User> getUserByIdFallback(@PathVariable("id") Long id){
        return RestResponse.fail("当前访问人数过多请稍后再试: getUserByIdFallback");
    }

    /**
     * 添加客户,即注册
     */
    @PostMapping("/")
    public RestResponse<User> insertUser(@RequestParam("username") String username,
                                         @RequestParam("password") String password,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("phone") String phone,
                                         @RequestParam("email") String email,
                                         @RequestParam("token") String token){
        User user = getUser(username,password,sex,phone,email,"customer",token);
        try{
            userService.save(user);
        } catch (Exception e){
            return RestResponse.fail("手机号已被注册");
        }
        return RestResponse.success(user);
    }

    /**
     * 添加员工
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/staff")
    public RestResponse<User> insertStaff(@RequestParam("username") String username,
                                         @RequestParam("password") String password,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("phone") String phone,
                                         @RequestParam("email") String email,
                                         @RequestParam("token") String token){
        User user = getUser(username,password,sex,phone,email,"staff",token);
        try{
            userService.save(user);
        } catch (Exception e){
            return RestResponse.fail("手机号已被注册");
        }
        return RestResponse.success(user);
    }


    /**
     * ...先不写那么完善了,密码验证啥的先不搞了
     */
    @PutMapping("/{id}")
    public RestResponse<User> updateUser(@PathVariable("id") Long id,
                                         @RequestParam("username") String username,
                                         @RequestParam("password") String password,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("phone") String phone,
                                         @RequestParam("email") String email,
                                         @RequestParam("iddentity") String iddentity,
                                         @RequestParam("token") String token){
        User user = getUser(username,password,sex,phone,email,iddentity,token);
        user.setId(id);
        userService.updateById(user);
        return RestResponse.success(user);
    }

    /**
     * 删除员工/拉黑顾客
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> deleteUser(@PathVariable("id") Long id){
        return RestResponse.success("删除成功",userService.removeById(id));
    }
    private User getUser(String username, String password, String sex, String phone, String email, String iddentity, String token){
        User user = new User();
        user.setUname(username);
        user.setPassword(MDUtil.MD5(password));
        user.setSex(sex.equals("男"));
        user.setPhone(phone);
        user.setEmail(email);
        user.setIddentity(iddentity.equals("customer"));
        String nowUsername = TokenUtil.getUserInfoFromToken(token);
        user.setCreateAuthor(nowUsername);
        user.setUpdateAuthor(nowUsername);
        return user;
    }

}
